package property.personal.property_mange.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import property.personal.property_mange.pojo.ParseData;
import property.personal.property_mange.pojo.Propertyinfo;
import property.personal.property_mange.service.impl.PagingImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import property.personal.property_mange.service.impl.ProperImpl;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class PagingController {

    @Autowired
    PagingImpl pagingimpl;

    @Autowired
    ProperImpl properimpl;

    @RequestMapping("/getdata")
    @ResponseBody
    public ParseData getdata(@RequestParam("page") int page,@RequestParam("limit") int limit,@RequestParam(value = "searchname",required = false) String propertyname){
        IPage<Propertyinfo> ipage;
        if (propertyname==null){
            ipage=properimpl.getpagedata(page,limit);
        }else{
            ipage=properimpl.getpagedatasearch(page,limit,propertyname);
        }
        return new ParseData(0,"分页成功",ipage.getTotal(),ipage.getRecords());
    }

    @ResponseBody
    @RequestMapping("/updatebyid")
    public Integer updatebyid(@RequestParam("id") String id,@RequestParam("status") String status,@RequestParam("code") int code,HttpServletResponse response) throws IOException {
        System.out.println("id="+id+"status"+status+"code="+code);
        int n=properimpl.updatebyid(id,status,code);

        return n;
    }

    @ResponseBody
    @RequestMapping("/updateby")
    public int update(@RequestParam("columname") String columname,@RequestParam("value") String value,@RequestParam("id") String id){
        int n=properimpl.updateby(id,columname,value);
        return n;
    }

    @RequestMapping("/asc")
    @ResponseBody
    public ParseData asc(@RequestParam("page") int page,@RequestParam("limit") int limit,@RequestParam("order") String order,@RequestParam("searchname") String propertyname){
        IPage<Propertyinfo> ipage;
        if (propertyname==null){
            ipage=properimpl.asc(page,limit,order);
        }else{
            ipage=properimpl.ascsearch(page,limit,order,propertyname);
        }

        return new ParseData(0,"分页成功",ipage.getTotal(),ipage.getRecords());
    }

}
